import 'package:flutter/material.dart';
import 'package:flutter_mode/other/permission_demo2.dart';
import 'package:flutter_mode/utils.dart';
import 'package:flutter_mode/zyj/ui/permission.dart';
//import 'package:permission/permission.dart';

class PermissionDemoPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var items = {
      'permission_handler(似乎IOS有些问题)': () => pushMaterial(context, PermissionPage()),
      'permission': () => pushMaterial(context, PermissionPage2()),
      'simple_permissions': ()=> pushMaterial(context, PermissionPage3()),
      'permission对比simple_permissions：simple_permissions获取权限状态较多。':(){}
    };

    return Scaffold(
      appBar: AppBar(
        title: Text(''),
      ),
      body: ListView.separated(
        itemBuilder: (context, position) {
          var item = items.entries.elementAt(position);
          return ListTile(
            title: Text(item.key),
            onTap: item.value,
          );
        },
        itemCount: items.length,
        separatorBuilder: (context, position) => Divider(
              height: 1,
            ),
      ),
    );
  }
}

class PermissionPage2 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _PermissionPage2State();
  }
}

class _PermissionPage2State extends State<PermissionPage2> {
  var listItems = {};

  /// 加载权限信息
  _loadPemissionInfo() async {
    /*var permissionItems = {
      PermissionName.Camera: () => _requestPermission(PermissionName.Camera),
      PermissionName.Storage: () => _requestPermission(PermissionName.Storage),
      PermissionName.Location: () =>
          _requestPermission(PermissionName.Location),
    };

    List<Permissions> permissions;
    try {
      permissions =
          await Permission.getPermissionStatus(List.of(permissionItems.keys));
    } catch (e) {
      print(e);
      permissions = [];
    }

    listItems.clear();

    for (var i = 0; i < permissionItems.length; i++) {
      var per = permissions[i];
      var item = permissionItems.entries.elementAt(i);
      listItems[
              '${per.permissionName}(${_resolvPermission(per.permissionStatus)})'] =
          item.value;
    }*/
  }

  /// 请求权限
  /*_requestPermission(PermissionName permissionName) async {
    final result = await Permission.requestSinglePermission(permissionName);
    showAlert(context, '请求权限$permissionName,得到状态${_resolvPermission(result)}');

    setState(() {});
  }

  /// 权限状态
  String _resolvPermission(PermissionStatus permissionStatus) {
    switch (permissionStatus) {
      case PermissionStatus.allow:
        return 'allow';
      case PermissionStatus.deny:
        return 'deny';
      case PermissionStatus.noAgain:
        return 'noAgain';
      default:
        return 'unKnown';
    }
  }*/

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return Scaffold(
      appBar: AppBar(
        title: Text('PermissionDemo'),
        actions: <Widget>[
          IconButton(
              icon: Icon(Icons.settings),
              onPressed: () => {/*Permission.openSettings()*/})
        ],
      ),
      body: FutureBuilder(
          future: _loadPemissionInfo(),
          builder: (context, snapshot) {
            return ListView.separated(
              itemBuilder: (context, position) {
                var item = listItems.entries.elementAt(position);
                return ListTile(
                  title: Text(item.key),
                  onTap: item.value,
                );
              },
              itemCount: listItems.length,
              separatorBuilder: (context, position) => Divider(
                    height: 1,
                  ),
            );
          }),
    );
  }
}